/*******************************************************************************
* This class provides a way to handle images.
*  
* Images with 1-, 4-, 8-, 16-, 24- or 32-bpp.
* Pixels are stored uncompressed, starting in the lower left corner,
* going from left to right, and then row by row from the bottom to the top of
* the image. Each row of pixels is extended to a 32-bits (4-byte) boundary so
* that the next row will start on a multiple-of-four byte location in memory.
* 
* Licensed under GPL Version 3 license (/license.txt).
* Author: Eder A. Perez (eder.perez@ice.ufjf.br)
*******************************************************************************/


#ifndef __CV_IMAGE_H__
#define __CV_IMAGE_H__

#include <stdio.h>
#include "cvdatatypes.h"
#include "cverror.h"


class CVImage {


  /*******************************************************************************/
  /* Attributes                                                                  */
  /*******************************************************************************/
  public:
    unsigned int width;
    unsigned int height;
    unsigned int bpp;      // Bits per pixel
    unsigned int rowsize;  // Length of a scanline in bytes (including padding, if it exists)
    CVubyte_t    *data;    // Matrix of image's pixels
    CVpixel_fmt_t pxlfmt; // Image's color format

  private:
    unsigned int palclrs;  // Number of colors in the palette
    CVubyte_t    *palette; // Used with 1-, 4- and 8-bpp images (palette is RGBA)

  public:
    CVerror_t errorcode;



  /*******************************************************************************/
  /* Methods                                                                     */
  /*******************************************************************************/
  public:
    // Constructors
    CVImage();
    ~CVImage();

    // Image load, save and create
    bool loadBMP(const char *filename);
    bool saveBMP(const char *filename);
    bool loadJPG(const char *filename);
    // Must be implemented: loadJPGFromData(); // Uncompress a jpg data from memory
    bool saveJPG(const char *filename, int quality);
    bool createImage(unsigned int width, unsigned int height, CVcolor_fmt_t clr_fmt);
    bool verticalFlip();

  private:
    //Unpack compressed/uncompressed data from BMP files
    CVerror_t unpack_bmp_24bpp(FILE* data_file, bool bottomup);
    CVerror_t unpack_bmp_16bpp(FILE* data_file, bool bottomup);
    CVerror_t unpack_bmp_8bpp (FILE* data_file, void *dib_header, void *bmp_header);
    CVerror_t unpack_bmp_4bpp (FILE* data_file, void *dib_header, void *bmp_header);
    CVerror_t unpack_bmp_1bpp (FILE* data_file, bool bottomup, void *dib_header);

};

#endif
